<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesProdOthercodePrintHistoryTable"
      >
      </ele-page>
    </div>
    <el-dialog
      width="20%"
      :title="$t('mesProdOtherCode.print')"
      :visible.sync="innerVisible"
      :append-to-body="true"
      @close="clearFormDialog()"
      v-loading="loading"
      :close-on-click-modal="false"
    >
      <el-form :model="form">
        <el-form-item
          :label="$t('mesProdOtherCode.printingTimes')"
          label-width="85px"
        >
          <el-input
            size="small"
            disabled
            v-model="form.printingTimes"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearFormDialog()">{{
          $t("common.cancel")
        }}</el-button>
        <el-button type="primary" @click="handleRequest">{{
          $t("common.submit")
        }}</el-button>
      </div>
    </el-dialog>

    <repairPrintTable ref="repairPrintTables"></repairPrintTable>

    <!-- 补打表单弹窗-->
    <ele-form-dialog
      :title="$i18n.t('mesOtherCodePrintingRecord.reprint')"
      v-loading="loading"
      :visible.sync="repairPrintDialogVisible"
      v-bind="repairFormDescDialog"
      v-model="repairFormDataDialog"
      :request-fn="handleSubmitRepair"
      :dialogAttrs="{
        'close-on-click-modal': false,
      }"
      size="50%"
      ref="eleFormDrawer"
    >
    </ele-form-dialog>

    <!-- 补打列表抽屉 -->
    <el-drawer
      :title="$i18n.t('mesOtherCodePrintingRecord.make-upList')"
      v-loading="loading"
      :visible.sync="repairListDialogVisible"
      size="50%"
      ref="eleFormDrawer"
      class="repair-print"
      :destroy-on-close="true"
      :wrapperClosable="false"
    >
      <ele-page
        v-loading="loading"
        :formConfig="repairSearchConfig"
        ref="repairPrintTable"
        :operateList="operateRepairList"
        @handleOperateEvent="handleOperateRepairEvent"
        :column="repairColumn"
        :getList="getRepairPrintList"
        uniqueId="repairPrintTable"
        :initForm="initRepairForm"
      ></ele-page>
      <div class="dialog-footer2">
        <el-button @click="repairListDialogVisible = false">
          {{ $t("common.cancel") }}
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialog } from "./config";
import { repairSearchConfig } from "@/views/print/mesOtherCodePrinting/config";
import {
  getList,
  selectListByConfigurationCode,
  printTemplateInfoName,
  getPrintingTimes,
} from "@/api/print/mesProdOthercodePrintHistory";
import { uniqueKey } from "@/utils/judge.js";
import repairPrintTable from "./components/repairPrintTable.vue";
import { getDict } from "@/api/common";
import repairMixin from "@/views/print/mesOtherCodePrinting/repairMixin";

export default {
  name: "mesProdOtherCodePrintHistory",
  components: {
    repairPrintTable,
  },
  mixins: [repairMixin, repairSearchConfig],
  data() {
    return {
      innerVisible: false,
      printRow: [],
      form: { printingTimes: 1 },
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, // 新增修改框
      searchConfig,
      operateList,
      getList,
      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
          fixed: "left",
        },
        {
          type: "index",
          label: this.$i18n.t("common.order"),
          fixed: "left",
          width: 60,
        },
        {
          prop: "barcode",
          label: this.$i18n.t("mesOtherCodePrintingRecord.barcode"),
          fixed: "left",
          minWidth: 350,
          showOverflowTooltip: true,
        },
        {
          prop: "codeType",
          label: this.$i18n.t("mesOtherCodePrintingRecord.codeType"),
          fixed: "left",
          width: 120,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict("mes_code_type");
            return list.data.result;
          },
        },
        {
          prop: "planCode",
          label: this.$i18n.t("mesOtherCodePrintingRecord.planCode"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "orderCode",
          label: this.$i18n.t("mesOtherCodePrintingRecord.orderCode"),
          minWidth: 240,
          showOverflowTooltip: true,
        },
        {
          prop: "prodCode",
          label: this.$i18n.t("mesOtherCodePrintingRecord.prodCode"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "prodName",
          label: this.$i18n.t("mesOtherCodePrintingRecord.prodName"),
          minWidth: 280,
          showOverflowTooltip: true,
        },
        {
          prop: "seriesCode",
          label: this.$i18n.t("mesOtherCodePrintingRecord.seriesCode"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "seriesName",
          label: this.$i18n.t("mesOtherCodePrintingRecord.seriesName"),
          minWidth: 180,
          showOverflowTooltip: true,
        },

        {
          prop: "barcodePrintTemp",
          label: this.$i18n.t("mesOtherCodePrintingRecord.barcodePrintTemp"),
          minWidth: 170,
          showOverflowTooltip: true,
        },
        {
          prop: "printtime",
          label: this.$i18n.t("mesOtherCodePrintingRecord.printTime"),
          width: 180,
        },
        {
          prop: "printtype",
          label: this.$i18n.t("mesOtherCodePrintingRecord.printType"),
          minWidth: 120,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict("printType");
            return list.data.result;
          },
        },
        {
          prop: "userName",
          label: this.$i18n.t("mesOtherCodePrintingRecord.printByName"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "createByName",
          label: this.$i18n.t("common.createByName"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "createTime",
          label: this.$i18n.t("common.createTime"),
          width: 180,
        },
        {
          prop: "updateByName",
          label: this.$i18n.t("common.updateByName"),
          minWidth: 180,
          showOverflowTooltip: true,
        },
        {
          prop: "updateTime",
          label: this.$i18n.t("common.updateTime"),
          width: 180,
        },
      ],
    };
  },
  computed: {},
  methods: {
    // 表格顶部按钮操作事件
    handleOperateEvent({ handleName, name, rows }) {
      this.title = name;
      const fnc = {
        repairPrint: async () => {
          const workshopList = uniqueKey(rows, "workshopCode");
          if (workshopList.length > 1) {
            this.$message.warning(
              $i18n.t("mesOtherCodePrintingRecord.sameWorkshop")
            );
            return;
          }
          const lineList = uniqueKey(rows, "lineCode");
          if (lineList.length > 1) {
            this.$message.warning(
              $i18n.t("mesOtherCodePrintingRecord.sameLine")
            );
            return;
          }
          const codeTypeList = uniqueKey(rows, "codeType");
          if (codeTypeList.length > 1) {
            this.$message.warning(
              $i18n.t("mesOtherCodePrintingRecord.sameCodeType")
            );
            return;
          }
          this.printRow = rows;
          // ---------获取打印次数-----------------
          // let res1 = await getPrintingTimes({
          //   goodsCode: data.rows[0].prodCode
          // })
          // if (res1.data.result && res1.data.result.frequency) {
          //   this.form.printingTimes = 1
          // }
          let params = {
            orderCode: rows[0].orderCode,
            codeType: "customer_code",
            configurationCode: "com_customer_code_reviewed",
          };
          let { workshopCode, lineCode } = rows[0];
          if (lineCode) {
            params.levelType = "5";
            params.lineCode = lineCode;
          } else {
            params.levelType = "4";
            params.workshopCode = workshopCode;
          }
          let res = await selectListByConfigurationCode(params);
          this.loading = false;
          this.isNeedCheck = false;
          if (res.data.result.length && res.data.result[0].value === "1") {
            this.isNeedCheck = true;
          }
          if (this.isNeedCheck) {
            this.initRepairForm = {
              orderCode: rows[0].orderCode,
              prodCode: rows[0].prodCode,
              prodName: rows[0].prodName,
            };
            this.repairListDialogVisible = true;
          } else {
            this.repairFormDataDialog = {
              orderCode: rows[0].orderCode,
              prodCode: rows[0].prodCode,
              prodName: rows[0].prodName,
            };
            this.repairPrintDialogVisible = true;
          }
        },
      };
      fnc[handleName] && fnc[handleName]();
    },
    clearFormDialog() {
      this.innerVisible = false;
      this.form.printingTimes = 0;
    },
    async handleRequest() {
      const barcodePrintTempList = uniqueKey(this.printRow, "barcodePrintTemp");
      if (barcodePrintTempList.length > 1) {
        this.$message.warning(
          $i18n.t("mesOtherCodePrintingRecord.sameBarcodePrintTemp")
        );
        return;
      }
      // 补打已打印其它码
      const temp = await printTemplateInfoName({
        templateName: barcodePrintTempList[0].barcodePrintTemp,
      });
      const barcodeInfo = {
        ...temp.data.result[0],
        values: this.printRow,
      };
      this.innerVisible = false;
      // ----条码打印多份（条码数量根据打印次数printingTimes重新计算）----
      let newInfoList = [];
      barcodeInfo.values.forEach((v) => {
        newInfoList = [
          ...newInfoList,
          ...Array(Number(this.form.printingTimes)).fill(v),
        ];
      });
      barcodeInfo.values = newInfoList;
      this.refresh();
      localStorage.splitPrint = JSON.stringify(barcodeInfo);
      window.open("/mom/component/mes/splitPrint?type=10");
    },
    // 表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name;
      const fnc = {};
      fnc[handleName]();
    },
    // 刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset);
    },
  },
};
</script>

<style lang="scss" scoped>
.repair-print {
  /deep/ .el-drawer__body {
    padding: 10px;
  }

  /deep/ .ele-form-search {
    height: auto !important;
  }
}
.dialog-footer2 {
  padding-top: 0px;
  display: flex;
  justify-content: flex-start;
  margin-top: -35px;
}
</style>
